var doctemp;
var mtDocument;


/**
 * 打开word文档
 */
function open_word() {

	var args = window.dialogArguments;

	doctemp = args[0];
	mtDocument = args[1];

	var TANGER_OCX_OBJ = document.getElementById("TANGER_OCX");
	var buttondiv = document.createElement("div");
	buttondiv.id = "toolbar";
	document.body.insertBefore(buttondiv, document.body.childNodes[0]);
	
	var tb = new Ext.Toolbar();
	
	var printAction = new Ext.Action({
					text : '<span style="font-size: 14px" title="打印当前文件内容">打印文件</font>',
					handler : function() {
						
					},
					iconCls : 'print'
				});

		tb.add(printAction);

	if (tb) {
		tb.render('toolbar');
	}

	TANGER_OCX_OBJ.OpenFromURL(doctemp);
	init_word(mtDocument, TANGER_OCX_OBJ);
}

function close_word() {
}

/**
 * 初始化保存正文时需要传入servlet的参数
 * 
 * @param {Object}
 *            paraObj
 */
function TANGER_OCX_FormPara(paraObj) {
	var str = 'columnName=' + columnName + '&';
	str += 'tableName=' + tableName + '&';
	str += 'docid=' + docid + '&';
	str += 'sequenceA=' + sequenceA;

	return str;
}

/**
 * 控件只读
 * 
 * @param {}
 *            boolvalue
 */
function TANGER_OCX_SetReadOnly(boolvalue) {
	// debugger;
	var i;
	try {
		if (boolvalue)
			TANGER_OCX_OBJ.IsShowToolMenu = false;
		with (TANGER_OCX_OBJ.ActiveDocument) {
			if (TANGER_OCX_OBJ.DocType == 1) // word
			{
				if ((ProtectionType != -1) && !boolvalue) {
					Unprotect();
				}
				if ((ProtectionType == -1) && boolvalue) {
					Protect(2, true, "");
				}
			} else if (TANGER_OCX_OBJ.DocType == 2)// excel
			{
				for (i = 1; i <= Application.Sheets.Count; i++) {
					if (boolvalue) {
						Application.Sheets(i).Protect("", true, true, true);
					} else {
						Application.Sheets(i).Unprotect("");
					}
				}
				if (boolvalue) {
					Application.ActiveWorkbook.Protect("", true);
				} else {
					Application.ActiveWorkbook.Unprotect("");
				}
			}
		}
	} catch (err) {
		// alert("\u9519\u8bef\uff1a" + err.number + ":" + err.description);
	} finally {
	}
}

/**
 * 初始化word
 * 
 * @param obj 公文表单document
 * @param ntkoObj ntko activeX object
 */
function init_word(obj, ntkoObj) {

	var i, j, k;
	var wh_name = "";
	var elementArr = new Array("INPUT", "SELECT", "TEXTAREA");
	
		
	for (j = 0; j < elementArr.length; j++) {
		var tagObj = obj.getElementsByTagName(elementArr[j]);

		for (i = 0; i < tagObj.length; i++) {

			CopyValueToBookMark(tagObj[i].name, tagObj[i].name, obj,
					ntkoObj);
		}
	}
	try {
			ntkoObj.ActiveDocument.CommandBars("NTKO安全签名印章").Visible = false;

		} catch (err) {
	}

		/*for (k = 0; k < elementArr.length; k++) {
			var tagObj = obj.getElementsByTagName(elementArr[k]);

			for (i = 0; i < tagObj.length; i++) {

				if (tagObj[i].special == "number") {
					wh_name = tagObj[i].name;
					break;
				}
			}
		}*/
		
		

}

/**
 * 将表单页面上指定输入域的value拷贝到正文中对应的标签中
 * 
 * @param {Object}
 *            inputname 表单页面上指定输入域的名称
 * @param {Object}
 *            BookMarkName 正文中对应的标签名称
 */
function CopyValueToBookMark(inputname, BookMarkName, obj, ntkoObj) {
	try {
		var inputValue = "";
		var j, elObj, optionItem;
		var elObj = obj.getElementById(inputname);
		if (!elObj) {
			return;
		}
		// 根据当前对象类型的不同，获取对象的value
		switch (elObj.type) {
			case "select-one" :
				inputValue = elObj.options[elObj.selectedIndex].text;
				break;
			case "select-multiple" :
				var isFirst = true;
				for (j = 0; j < elObj.options.length; j++) {
					optionItem = elObj.options[j];
					if (optionItem.selected) {
						if (isFirst) {
							inputValue = optionItem.text;
							isFirst = false;
						} else {
							inputValue += "  " + optionItem.text;
						}
					}
				}

				break;
			case "checkbox" :
				var eles = obj.getElementsByName(inputname);
				if (eles != null) {
					for (var k = 0; k < eles.length; k++) {
						if (eles[k].checked == true) {
							// alert("eles[k].value======"+eles[k].value);
							inputValue = eles[k].value;
							break;
						}
					}
				}

				break;
			default : // text,Areatext,selecte-one,password,submit,etc.
				inputValue = elObj.value;
				break;
		}

		var mybt = "";
		if (obj.getElementById("fwbt") != null) {
			mybt = obj.getElementById("fwbt").value;
		}

		if (inputname == "keyBT" && inputValue == mybt) {
		} else {

			// TANGER_OCX_OBJ = document.all.item("TANGER_OCX");
			// 将对象的value拷贝到对应的标签对象中
			var bkmkObj = ntkoObj.ActiveDocument.BookMarks(BookMarkName);
			// var tacheid = opener.document.getElementById("tacheid").value;

			if (!bkmkObj) {
			}
			var saverange = bkmkObj.Range;
			var inputValue_array = new Array();
			var yj_fgf = "@";
			var fj_fgf = "*";
			if(inputname.indexOf("_showlabel")>-1){
				if(inputValue.indexOf(yj_fgf)>-1){
					inputValue = getZfc(inputValue,yj_fgf);
				}else if(inputValue.indexOf(fj_fgf)>-1){
					inputValue = getZfc(inputValue,fj_fgf);
				}
			}
			saverange.Text = inputValue;
			if (inputname == "keyZTC") {
				saverange.Font.NameFarEast = "方正小标宋简体";
				saverange.Font.NameAscii = "方正小标宋简体";
			}

			ntkoObj.ActiveDocument.Bookmarks.Add(BookMarkName, saverange);

		}

	} catch (err) {

	}
}
/**
 * 特殊域往正文带值操作
 * 
 * @param {}
 *            obj
 * @param {}
 *            wh_name
 */
function tsFiled_word(obj, wh_name, ntkoObj) {

	// TANGER_OCX_OBJ = document.all.item("TANGER_OCX");
	if (ntkoObj.ActiveDocument.Bookmarks.Exists("wh")) {
		var bkmkObj1 = ntkoObj.ActiveDocument.BookMarks("wh");
		var saverange1 = bkmkObj1.Range;
		saverange1.Text = obj.getElementById(wh_name).value;
		ntkoObj.ActiveDocument.Bookmarks.Add("wh", saverange1);
	}
}

/**
 * 显示签章按钮判断
 * 
 * @param {}
 *            obj
 */
function display_gz(obj, ntkoObj) {
	// TANGER_OCX_OBJ = document.all.item("TANGER_OCX");

}

/**
 * 判断文档是否有盖章
 */
function judge_gz(ntkoObj) {
	var judgeGz = false;
	// TANGER_OCX_OBJ = document.all.item("TANGER_OCX");
	var myDoc = ntkoObj.ActiveDocument;
	var shapes = myDoc.shapes;
	for (i = 1; i <= shapes.Count; i++) {
		var curShape = shapes(i);
		if (12 == curShape.Type) {

			if ("NTKO.SecSignControl".toUpperCase() == curShape.OLEFormat.ClassType
					.toUpperCase()) {

				// **********
				judgeGz = true;
				break;

			}

		}

	}
	return judgeGz;
}

function getLast_TableBottom(ntkoObj) {
	if (true) {
		var mydoc = ntkoObj.ActiveDocument;
		var sel = mydoc.Application.Selection;
		var docshapes = mydoc.Shapes;
		var table_len = mydoc.Tables.Count;
		var last_table = mydoc.Tables(table_len);
		last_table.Range.Select();
		// *********************************************让版记自动沉底
		last_table.Rows.WrapAroundText = true;
		last_table.Rows.RelativeHorizontalPosition = 0;
		last_table.Rows.VerticalPosition = -999997;
		last_table.Rows.RelativeVerticalPosition = 0;
		last_table.Rows.AllowOverlap = true;
		sel.EndKey(5);
	}

}


function TANGER_OCX_SetWordname(ntkoObj)
{
	if(ntkoObj.ActiveDocument.ProtectionType == -1){
	}
	
}

//切换打开或者关闭修订模式
function TANGER_OCX_SetReviewMode(ntkoObj,boolvalue)
{
	if(ntkoObj.ActiveDocument.ProtectionType == -1){
		
		ntkoObj.ActiveDocument.TrackRevisions = boolvalue;
	}
	
}

//显示/不显示修订文字
function TANGER_OCX_ShowRevisions(ntkoObj,boolvalue)
{
	if(ntkoObj.ActiveDocument.ProtectionType == -1){
		
		ntkoObj.ActiveDocument.ShowRevisions = boolvalue;
	}
	
}

//得到指定分割符对应的字符串
function getZfc(total_zf,obj){
	var temp_array = total_zf.split(obj);
	var temp_total="";
	for(var i=0;i<temp_array.length;i++){
		if(temp_total==""){
			temp_total = temp_array[i];
		}else{
			temp_total = temp_total+"\n"+temp_array[i];
		}
	}
	return temp_total;
	
}
